//
//  MLClassPropManager.m
//
//  Created by Molon on 13-12-10.
//  Copyright (c) 2013年 Molon. All rights reserved.
//

#import "MLClassPropManager.h"
#import "MLRuntime.h"

@interface MLClassPropManager()

@property (nonatomic, strong) NSMutableDictionary *props;

@end

@implementation MLClassPropManager

+ (instancetype)manager {
    static MLClassPropManager *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self class] new];
    });
    return _sharedInstance;
}

- (NSMutableDictionary*)props
{
    if (!_props) {
        _props = [NSMutableDictionary dictionary];
    }
    return _props;
}

#pragma mark - haskey
- (BOOL)hasKey:(NSString*)key ofClass:(Class)cls
{
    return [self hasKey:key ofClass:cls untilSuperClass:cls ignoreUntilClass:NO];
}

- (BOOL)hasKey:(NSString*)key ofClass:(Class)cls untilSuperClass:(Class)supercls
{
    return [self hasKey:key ofClass:cls untilSuperClass:supercls ignoreUntilClass:NO];
}

- (BOOL)hasKey:(NSString*)key ofClass:(Class)cls untilSuperClass:(Class)supercls ignoreUntilClass:(BOOL)ignoreUntilClass
{
    NSDictionary *dict = [self propToTypesOfClass:cls untilSuperClass:supercls ignoreUntilClass:ignoreUntilClass];
    if (dict[key]!=nil) {
        return YES;
    }
    return NO;
}

#pragma mark - prop
- (NSDictionary *)propToTypesOfClass:(Class)cls
{
    NSString *clsname = NSStringFromClass(cls);
    if (!self.props[clsname]) {
        [self.props setValue:GetPropToTypesOfClass(cls) forKey:clsname];
    }
    return self.props[clsname];
}

- (NSDictionary *)propToTypesOfClass:(Class)cls untilSuperClass:(Class)supercls
{
    return [self propToTypesOfClass:cls untilSuperClass:supercls ignoreUntilClass:NO];
}

- (NSDictionary *)propToTypesOfClass:(Class)cls untilSuperClass:(Class)supercls ignoreUntilClass:(BOOL)ignoreUntilClass
{
    //相同的话就直接返回当前的
    if ([NSStringFromClass(cls) isEqualToString:NSStringFromClass(supercls)]) {
        if (ignoreUntilClass) {
            return nil;
        }
        return [self propToTypesOfClass:cls];
    }
    
    NSAssert([cls isSubclassOfClass:supercls], @"%@父类传递错误",NSStringFromSelector(_cmd));
    
    Class supersupercls = ignoreUntilClass?supercls:[supercls superclass]; //如果包括当前supercls 所以需要如此
    if (!supersupercls) {
        supersupercls = [NSObject class];
    }
    
    NSString *clsname =  [NSString stringWithFormat:@"%@-%@",NSStringFromClass(cls),NSStringFromClass(supersupercls)];
    if (!self.props[clsname]) { //里面存储的都是不包括supersupercls的
        NSMutableDictionary *result = [NSMutableDictionary dictionary];
        Class currentcls = cls;
        do {
            NSMutableDictionary *properties = [[self propToTypesOfClass:currentcls]mutableCopy];
            
            //移除重复的，以子类的为基准
            NSArray *allKeys = [result allKeys];
            [properties removeObjectsForKeys:allKeys];
            
            [result addEntriesFromDictionary:properties];
            
            currentcls = [currentcls superclass];
        } while (currentcls&&![NSStringFromClass(currentcls) isEqualToString:NSStringFromClass(supersupercls)]);
        
        
        [self.props setValue:result forKey:clsname];
    }
    return self.props[clsname];
}

@end
